
$(function () {
    $("#update").click(function () {
        $(".s-box").hide();
        $(".s-box1").show();
        $.ajax({
            url:"/sdkd/userInfo/selectUserInfo.do",
            type:"post",
            async: false,
            dataType:"json",
            success:function (data) {
                $("#headImg").attr("src","http://qlhc5wmuf.hn-bkt.clouddn.com" +data.data.headPortrait)
                $("#nickname").val(data.data.userName)
                $("#idcardnum").val(data.data.idCard)
                if("男" == data.data.sex){
                    $("#male").attr("checked","checked")
                }else {
                    $("#female").attr("checked","checked")
                }
                $("#age").val(data.data.age)
                $("#homeaddress").val(data.data.address)
            }
        })
    })

    $("#submit").click(function () {
        $(".s-box1").hide();
        $(".s-box").show();
        if($("#male").attr("checked")){
            var sex ="男"
        }else if($("#female").attr("checked")) {
            var sex ="女"
        }
        let user = {"userName":$("#nickname").val(),"idCard":$("#idcardnum").val(),"sex":sex,"age":$("#age").val(),"address":$("#homeaddress").val()}
        console.log(user)
        let formData = new FormData();
        formData.append("file", $("#headPortrait")[0].files[0]);
        formData.append("userName",$("#nickname").val());
        formData.append("idCard",$("#idcardnum").val());
        formData.append("sex",sex);
        formData.append("age",$("#age").val());
        formData.append("address",$("#homeaddress").val());
        console.log(formData.get("file"))
        console.log(typeof formData.get("file"))

        //传信息
        $.ajax({
            url:"/sdkd/userInfo/updateUserInfo.do",
            type:"post",
            data:formData,
            // async: false,
            cache:false, //上传文件无需缓存
            processData:false, // 用于对参数进行序列化处理，这里必须设为false
            contentType:false,
            dataType:"json",
            success:function (data) {
                console.log(data)
                location.href ="/sdkd/personalcenter/html/PersonalData.html"
            }
        })
    })
})

// 找到头像的input标签绑定change事件
$("#headPortrait").change(function () {
    // 1. 创建一个读取文件的对象
    var fileReader = new FileReader();
    // 取到当前选中的头像文件
    // console.log(this.files[0]);
    // 读取你选中的那个文件
    fileReader.readAsDataURL(this.files[0]);  // 读取文件是需要时间的
    fileReader.onload = function () {
        // 2. 等上一步读完文件之后才 把图片加载到img标签中
        $("#headImg").attr("src", fileReader.result)
    }
})


$.ajax({
    url:"/sdkd/userInfo/selectUserInfo.do",
    type:"post",
    async: false,
    dataType:"json",
    success:function (data) {
        console.log(data)
        $("#userImg").attr("src","http://qlhc5wmuf.hn-bkt.clouddn.com" +data.data.headPortrait)
        $("#userName").text(data.data.userName)
        $("#idCard").text(data.data.idCard)
        $("#sex").text(data.data.sex)
        $("#ageInfo").text(data.data.age)
        $("#address").text(data.data.address)
    }
})

